package com.eastcom.mystope.daemon;


import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.eastcom.mystope.common.util.SpringContextProvider;
import com.eastcom.mystope.daemon.config.BasePath;
import com.eastcom.mystope.daemon.config.ExternConfigPath;
import com.eastcom.mystope.daemon.config.ServerPath;
import com.eastcom.mystope.daemon.context.ServerContext;
import com.eastcom.mystope.daemon.core.app.Application;
import com.eastcom.mystope.daemon.core.process.ServerInitProcess;
import com.eastcom.mystope.daemon.tools.JaxbUtil;

public class Launcher extends Application{
	private static Object lock = new Object();
	private static Log logger = LogFactory.getLog(Launcher.class);

	protected void mainloop() {

		logger.info("[I]" + getConfig().getAppName() + " start running ");

		ServerInitProcess serverInitProcess = SpringContextProvider.getBean("serverInitProcess");
		serverInitProcess.init();
		logger.debug("heheheddh");
		synchronized (lock) {
			try {
				lock.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	protected ArrayList<?> displayStatus(String command) {
		// TODO Auto-generated method stub
		return null;
	}

	protected boolean preInitialize() {
		ServerPath serverPath = JaxbUtil.converyFileToJavaBean(args[1],"UTF-8", ServerPath.class);
		ServerContext.serverPath = serverPath;
		if(serverPath.getBaseConfigPath() == null){
			logger.error("服务初始化文件路径不对。");
			return false;
		}
		return true;
	}

	protected void printUsage() {
		// TODO Auto-generated method stub

	}


	public static void main(String[] args) {
		if(args.length == 2){
			Launcher app = new Launcher();
			app.setArgs(args);
			app.run();
			logger.debug("luncher 正在退出......");
			System.exit(1);
		}
	}

	public static void notifyLock(){
		synchronized (lock) {
			lock.notifyAll();
		}
	}
}
